package com.kj.report.mongoRepository;

import java.util.List;

import com.kj.report.model.ReportView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;

/**
 * mn
 */

@Component
public class ReportViewRepository {
	@Autowired
	private MongoTemplate mongoTemplate;

	public List<ReportView> findAll() {

		return mongoTemplate.find(new Query(), ReportView.class);
	}

	/**
	 * 插入
	 * 
	 * @param reportView
	 */
	public void insert(ReportView reportView) {
		mongoTemplate.insert(reportView);
	}

	
	
	/**
	 * 根据userId 查询
	 * @param userId
	 * @return
	 */
    public List<ReportView> findByUserId(Integer userId) {
        Criteria criteria = new Criteria("userId").is(userId);
        return mongoTemplate.find(new Query(criteria), ReportView.class);

    }

	
	/**
	 * 根据报告ID 查询
	 * @param inspectId
	 * @return
	 */
	public ReportView findById(String inspectId) {
		 return mongoTemplate.findOne(new Query(Criteria.where("_id").is(inspectId)), ReportView.class);
	}

	
	/**
	 * 删除所有
	 */
    public void removeAll() {
        List<ReportView> list = this.findAll();
        if (list != null) {
            for (ReportView reportView : list) {
            	mongoTemplate.remove(reportView);
            }
        }

    }

    /**
     * 根据ID删除报告
     * @param id
     */
    public void removeOne(String id) {
        Criteria criteria = Criteria.where("id").in(id);
        if (criteria == null) {
            Query query = new Query(criteria);
            if (query != null && mongoTemplate.findOne(query, ReportView.class) != null)
            	mongoTemplate.remove(mongoTemplate.findOne(query, ReportView.class));
        }

    }


}
